package com.yanqu.road.dataHelper.manager;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ConfigMgr extends BaseMgr {

    static Map<Integer, String> shopMallIdNameMap = new ConcurrentHashMap<>();

    static Map<Integer, String> activityMallIdNameMap = new ConcurrentHashMap<>();

    static Map<String, String> langNameMap = new ConcurrentHashMap<>();

    static Map<Integer, Map<String, String>> secretVolumeMap = new ConcurrentHashMap<>();

    static Map<Integer, BigDecimal> buildingCostMap = new ConcurrentHashMap<>();
    static Map<Integer, BigDecimal> buildingStaffMap = new ConcurrentHashMap<>();

    public static List<Integer> mammonDecorationList = new ArrayList<>();

    //decorationId, Level
    public static Map<Integer, Integer> decorationLevelMap = new HashMap<>();

    public static Map<Integer, BanquetGiftInfo> banquetGiftInfoMap = new ConcurrentHashMap<>();

    public static Map<Integer, GoodsInfo> goodsInfoMap = new HashMap<>();

    public ConfigMgr() {
        initLangName();
        initMallShop();
        initActivityMallShop();
        initSecretVolume();
        initBuildingCostConfig();
        initBuildingStaffCost();
        initMammonDecoration();//加载财神称号
        initDecorationGoods();//加载称号等级
        initBanquetGiftInfo(); // 加载宴会礼物信息
        initGoods();
    }


    private void initGoods() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select * from t_p_goods");
            rs = call.executeQuery();
            while (rs.next()) {
                GoodsInfo data = new GoodsInfo();
                data.setGoodsId(rs.getInt("id"));
                data.setGoodsName(rs.getString("name"));
                data.setType(rs.getInt("type"));
                data.setQuality(rs.getInt("quality"));
                data.setCanUse(rs.getInt("canUse"));
                data.setTopLimit(rs.getInt("maxNum"));
                data.setSort(rs.getInt("sort"));
                data.setParamStr(rs.getString("params"));
                data.setParamList(StringUtils.stringToBigIntegerList(data.getParamStr(), "\\|"));
                data.setExtendParam(rs.getString("extendParam"));
                goodsInfoMap.put(data.getGoodsId(), data);
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }


    private void initBanquetGiftInfo() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select * from t_p_banquet_gift");
            rs = call.executeQuery();
            while (rs.next()) {
                BanquetGiftInfo data = new BanquetGiftInfo();
                data.setId(rs.getInt("id"));
                data.setType1(rs.getInt("type1"));
                data.setType2(rs.getInt("type2"));
                data.setJoinConsume(rs.getString("joinConsume"));
                data.setJoinScore(rs.getString("joinScore"));
                data.setJoinReward(rs.getString("joinReward"));

                banquetGiftInfoMap.put(data.getId(), data);
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }

    public static BigInteger getStaffCost(int buildingId, int count) {
        BigDecimal baseCost = buildingCostMap.get(buildingId);
        if (count >= 15000) {
            count = 15000;
        }
        BigDecimal param = buildingStaffMap.get(count);
        BigDecimal money = baseCost.multiply(param).divide(BigDecimal.valueOf(1000000),BigDecimal.ROUND_DOWN);
        return money.toBigInteger();
    }

    private void initBuildingStaffCost() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select staffCount,`consume` from t_p_building_staff");
            rs = call.executeQuery();
            while (rs.next()) {
                buildingStaffMap.put(rs.getInt("staffCount"), new BigDecimal(rs.getString("consume")));
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }

    private void initMammonDecoration() {
        ResultSet rs;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select id from t_p_decoration where type = 2");
            rs = call.executeQuery();
            while (rs.next()) {
                mammonDecorationList.add(rs.getInt("id"));
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }

    private void initDecorationGoods() {
        ResultSet rs;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select * from t_p_goods where id in( select id from t_p_decoration where type = 2 )");
            rs = call.executeQuery();
            while (rs.next()) {
                int id = rs.getInt("id");
                String extendParam = rs.getString("extendParam");
                int level = StringUtils.stringToIntegerList(extendParam, "\\|").get(4);
                decorationLevelMap.put(id, level);
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }

    }

    private void initBuildingCostConfig() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select id,`addStaffCost` from t_p_building");
            rs = call.executeQuery();
            while (rs.next()) {
                buildingCostMap.put(rs.getInt("id"), new BigDecimal(rs.getString("addStaffCost")));
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }

    public static String getMallName(int mallId) {
        if (shopMallIdNameMap.containsKey(mallId)) {
            return shopMallIdNameMap.get(mallId);
        }
        if (activityMallIdNameMap.containsKey(mallId)) {
            return activityMallIdNameMap.get(mallId);
        }
        return "未知";
    }

    private void initActivityMallShop() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall("select id,`name` from t_s_activity_mall");
            rs = call.executeQuery();
            while (rs.next()) {
                activityMallIdNameMap.put(rs.getInt("id"), rs.getString("name"));
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }

    private void initMallShop() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            String sql = "select t.id,t.rewards,t.littleType,tc.completeParams from t_p_task t " +
                    "left join t_p_task_condition tc on t.id = tc.Id " +
                    "where t.type = 7";
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String, String> infoMap = new ConcurrentHashMap<>();
                infoMap.put("id", rs.getString("id"));
                infoMap.put("rewards", rs.getString("rewards"));
                infoMap.put("littleType", rs.getString("littleType"));
                infoMap.put("completeParams", rs.getString("completeParams"));
                secretVolumeMap.put(rs.getInt("id"), infoMap);
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }

    private void initSecretVolume() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select id,`name` from t_p_mall");
            rs = call.executeQuery();
            while (rs.next()) {
                shopMallIdNameMap.put(rs.getInt("id"), getLangName(rs.getString("name")));
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }

    //根据值获取那些可以领奖
    public Map<String, Map<String, String>> getCangetRewardSecretVolumeMap(int value) {
        Map<String, Map<String, String>> retMap = new ConcurrentHashMap<>();
        for (Map<String, String> info : secretVolumeMap.values()) {
            List<Integer> completeParamsList = StringUtils.stringToIntegerList(info.get("completeParams"), "\\|");
            if (completeParamsList.get(3) <= value) {
                retMap.put(info.get("id"), info);
            }
        }
        return retMap;
    }
    public Map<String, Map<String, String>> getCanGetRewardSecretVolumeMap(int value) {
        Map<String, Map<String, String>> retMap = new ConcurrentHashMap<>();
        for (Map<String, String> info : secretVolumeMap.values()) {
            List<Integer> completeParamsList = StringUtils.stringToIntegerList(info.get("completeParams"), "\\|");
            if (completeParamsList.get(3) <= value) {
                Map<String,String> tempData = new ConcurrentHashMap<>();
                tempData.put("id",info.get("id"));
                tempData.put("rewards",info.get("rewards"));
                retMap.put(tempData.get("id"), tempData);
            }
        }
        return retMap;
    }

    public String getLangName(String name) {
        if (langNameMap.containsKey(name)) {
            return langNameMap.get(name);
        }
        return name;
    }

    private void initLangName() {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall("select id,`zh_cn`,`zh_tw` from t_p_multi_language");
            rs = call.executeQuery();
            while (rs.next()) {
                String name = rs.getString("zh_tw");
                if (name != null) {
                    langNameMap.put(rs.getString("id"), name);
                }
            }
        } catch (SQLException e) {
            logger.error("异常错误", e);
        } finally {
            getDb().closeConnection(call, null);
        }
    }
}
